/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import entities.Music;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sessions.MusicManager;

/**
 *
 * @author Antoine Boulinguez et Shyn-Yuan Cheng
 */
public class StreamingMp3Servlet extends HttpServlet {
    
    @EJB
    private MusicManager musicManager;    
    
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException 
    {
        //Uncomment for test purpose
        //String fileName="D:\\MesDocuments\\Musiques\\Adele - Skyfall.mp3";
        
        //Get the songID from the request
        //
        String songID = request.getParameter("songID");
        Long songID_long = Long.parseLong(songID);
        
        if(songID!=null)
        {
            //Get the song path
            //
            Music music = this.musicManager.getMusic(songID_long);
            if(music==null){return;}
            String filePath = music.getFilePath();
            String absoluteFilePath = request.getServletContext().getRealPath(filePath);
            
            File mp3 = new File(absoluteFilePath);
            if(mp3.exists())
            {
                ServletOutputStream stream = null;
                BufferedInputStream buf = null;
                try 
                {
                    stream = response.getOutputStream();

                    //set response headers
                    response.setContentType("audio/mpeg"); 

                    response.addHeader("Content-Disposition", "attachment; filename=" + absoluteFilePath);

                    response.setContentLength((int) mp3.length());

                    FileInputStream input = new FileInputStream(mp3);
                    buf = new BufferedInputStream(input);
                    int readBytes = 0;
                    //read from the file; write to the ServletOutputStream
                    while ((readBytes = buf.read()) != -1)
                    {
                        stream.write(readBytes);
                    }
                } 
                catch (IOException ioe) 
                {
                    throw new ServletException(ioe.getMessage());
                } 
                finally 
                {
                    if (stream != null){stream.close();}
                    if (buf != null){buf.close();}
                }
            }
        }
        else
        {
            //TODO
        }
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
